1   // Copyright 2006-2014 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   // http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package org.apache.tapestry5.internal.services;
16  
17  import org.apache.tapestry5.internal.test.InternalBaseTestCase;
18  import org.apache.tapestry5.ioc.Resource;
19  import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
20  import org.apache.tapestry5.ioc.util.UnknownValueException;
21  import org.apache.tapestry5.services.AssetAlias;
22  import org.apache.tapestry5.services.ClasspathAssetAliasManager;
23  import org.testng.annotations.DataProvider;
24  import org.testng.annotations.Test;
25  
26  import java.io.IOException;
27  import java.util.Map;
28  
29  public class ClasspathAssetAliasManagerImplTest extends InternalBaseTestCase
30  {
31      public Map<String, String> configuration()
32      {
33          Map<String, String> configuration = CollectionFactory.newMap();
34  
35          configuration.put("tapestry", "org/apache/tapestry5");
36          configuration.put("tapestry-internal", "org/apache/tapestry5/internal");
37          configuration.put("mylib", "com/example/mylib");
38  
39          return configuration;
40      }
41  
42      @Test
43      public void slash_not_allowed_as_alias()
44      {
45          Map<String, String> configuration = CollectionFactory.newMap();
46  
47          configuration.put("slash/at/end/", "com/myco/old/style/library");
48  
49          try
50          {
51              new ClasspathAssetAliasManagerImpl(configuration);
52              unreachable();
53          } catch (RuntimeException ex)
54          {
55              assertMessageContains(ex, "Contribution of folder name 'slash/at/end/' is invalid as it may not start with or end with a slash");
56          }
57      }
58  
59      @Test
60      public void get_mappings()
61      {
62          // Notice how all the trailing slashes (which are tolerated but not wanted)
63          // have been removed.
64  
65          Map<String, String> expected = CollectionFactory.newCaseInsensitiveMap();
66  
67          expected.put("tapestry", "org/apache/tapestry5");
68          expected.put("tapestry-internal", "org/apache/tapestry5/internal");
69          expected.put("mylib", "com/example/mylib");
70  
71          ClasspathAssetAliasManager manager = new ClasspathAssetAliasManagerImpl(configuration());
72  
73          assertEquals(manager.getMappings(), expected);
74      }
75  
76      @Test(dataProvider = "to_client_url_data")
77      public void to_client_url(String resourcePath, String expectedFolder, String expectedPath) throws IOException
78      {
79          Resource r = mockResource();
80  
81          expect(r.getPath()).andReturn(resourcePath);
82  
83          replay();
84  
85          ClasspathAssetAliasManager manager = new ClasspathAssetAliasManagerImpl(configuration());
86  
87          AssetAlias alias = manager.extractAssetAlias(r);
88  
89          assertEquals(alias.virtualFolder, expectedFolder);
90          assertEquals(alias.path, expectedPath);
91  
92          verify();
93      }
94  
95      @Test
96      public void can_not_map_resource_path_that_matches_virtual_folder() throws IOException
97      {
98          Resource r = mockResource();
99  
100         expect(r.getPath()).andReturn("com/example/mylib");
101 
102         replay();
103 
104         ClasspathAssetAliasManager manager = new ClasspathAssetAliasManagerImpl(configuration());
105 
106         try
107         {
108             manager.extractAssetAlias(r);
109 
110             unreachable();
111         } catch (IllegalArgumentException ex)
112         {
113 
114         }
115 
116         verify();
117 
118     }
119 
120     @Test
121     public void failure_if_path_not_in_mapped_alias_folder()
122     {
123         ClasspathAssetAliasManager manager = new ClasspathAssetAliasManagerImpl(configuration());
124         Resource resource = mockResource();
125 
126         expect(resource.getPath()).andReturn("org/example/icons/flag.gif").atLeastOnce();
127 
128         replay();
129 
130         try
131         {
132             manager.extractAssetAlias(resource);
133             unreachable();
134         } catch (UnknownValueException ex)
135         {
136             assertMessageContains(ex, "Unable to create a client URL for classpath resource org/example/icons/flag.gif");
137 
138             assertListsEquals(ex.getAvailableValues().getValues(), "com/example/mylib", "org/apache/tapestry5",
139                     "org/apache/tapestry5/internal");
140         }
141 
142         verify();
143     }
144 
145     @DataProvider
146     public Object[][] to_client_url_data()
147     {
148         return new Object[][]
149                 {
150                         {"com/example/mylib/Foo.bar", "mylib", "Foo.bar"},
151                         {"com/example/mylib/nested/Foo.bar", "mylib", "nested/Foo.bar"},
152                         {"org/apache/tapestry5/internal/Foo.bar", "tapestry-internal", "Foo.bar"},
153                         {"org/apache/tapestry5/Foo.bar", "tapestry", "Foo.bar"},
154                 };
155     }
156 
157 }